package com.dfbz.dao;

import com.dfbz.common.Constr;
import com.dfbz.dao.base.BaseDao;
import com.dfbz.model.UserFocus;
import com.dfbz.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserFocusDao extends BaseDao<UserFocus> {

    public UserFocusDao() {
        super(UserFocus.class);
    }

    public List<Map<String,Object>> getUserFocusList(String userId,int currentPage){
        List<Map<String, Object>> list = null;
        String sql = "select uf.id,ui.*\n" +
                "from user_focus uf join user_info ui on uf.user_focus_id=ui.user_id\n" +
                "where uf.user_focus_id=ui.user_id and uf.user_id = ?\n" +
                "limit ?,?";

        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());

        try {
            list = qr.query(sql, new MapListHandler(), userId,(currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    public long getUserFocusTotal(String userId) {
        long total=0L;
        String sql = "select count(*) as totalUserFocus from user_focus where user_id = ?;";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            map = qr.query(sql, new MapHandler(),userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null!=map){
            total = Long.parseLong(map.get("totalUserFocus").toString());
        }
        return total;
    }
}
